﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;


using Membership.Library.Web;
using System.ServiceModel.DomainServices.Client;



namespace Membership.UI
{
    public partial class MainPage : UserControl
    {
        MembershipDomainContext _context = new MembershipDomainContext();
        public MainPage()
        {
            InitializeComponent();
            CustomInitializeComponent();

        }

        public void CustomInitializeComponent()
        {
            var loadOp = _context.Load(_context.GetFuncitonQuery());
            loadOp.Completed += new EventHandler(loadOp_Completed);
        }

        #region 导航菜单
        void loadOp_Completed(object sender, EventArgs e)
        {
            var query = sender as LoadOperation<Function>;
            this.MenuTreeView.ItemsSource = BuildTreeQuick(query.Entities.ToList());
        }

        private IQueryable<Function> BuildTreeQuick(List<Function> treeData)
        {
            Dictionary<string, Function> List = new Dictionary<string, Function>();

            List<Function> Nodes = new List<Function>();//保存树视图结果

            foreach (Function nv in treeData)
            {
                if (!List.ContainsKey(nv.FunctionId))
                {
                    List.Add(nv.FunctionId, nv);
                }

                Nodes.Add(nv);//都追加 如果发现父节点 那么移除

            }

            //第二遍 循环 建立 父子 关系

            foreach (Function tn in List.Values)
            {
                Function en = tn;

                if (List.ContainsKey(en.ParentId))
                {
                    //找到 他的 父节点 
                    Function tpn = List[en.ParentId];


                    if (tpn != null && tpn.FunctionId != en.FunctionId)
                    {

                        Nodes.Remove(tn); //如果发现父节点 那么移除

                        if (tpn.Childrens == null) tpn.Childrens = new List<Function>();

                        en.Parent = tpn;

                        tpn.Childrens.Add(tn); //添加到父节点下面
                    }
                }
            }

            //第三边循环 建立 兄弟 导航关系

            return Nodes.AsQueryable();

        }

        #endregion

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {

        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void CollapseImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            if (fe.Opacity == 1)
            {
                this.hideMenu.Begin();
            }
            else
            {
                this.showMenu.Begin();
            }
        }
    }
}